package leetcode_core.leetcode_2;

public class MyQueue <V>{

    class ListNode <V>{
        V val;
        ListNode next;
        ListNode() {}
        ListNode(V val) { this.val = val; }
        ListNode(V val, ListNode next) { this.val = val; this.next = next; }
    }

    private ListNode<V> head;
    private ListNode<V> tail;
    private int size;

    public MyQueue() {
        head = tail = null;
        size = 0;
    }

    public void offer(V val){
        //如果是队列是空的
        if(head == null && tail == null){
            head = tail = new ListNode<V>(val);
        }else{
            //如果队列非空,那么就在队列尾部添加元素
            tail.next = new ListNode<>(val);
            tail = tail.next;
        }
        this.size++;
    }

    public V poll(){
        V val = null;
        if(head == null && tail == null){
            return null;
        }else{
            //从头部删除元素
            ListNode next = head.next;
            val= head.val;
            head = next;
        }
        this.size--;
        return val;
    }

    public V peek(){
        V ans = null;
        if(head != null){
            ans = head.val;
        }
        return ans;
    }


}
